package edu.nwpu.zya.coreops.modules.alert.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import edu.nwpu.zya.coreops.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import edu.nwpu.zya.coreops.modules.alert.entity.HistoryEntity;
import edu.nwpu.zya.coreops.modules.alert.service.HistoryService;
import edu.nwpu.zya.coreops.common.utils.PageUtils;
import edu.nwpu.zya.coreops.common.utils.R;

/**
 * 
 *
 * @author zhouyuang
 * @email 742025491@qq.com
 * @date 2022-04-27 21:26:24
 */
@RestController
@RequestMapping("alert/history")
public class HistoryController {
    @Autowired
    private HistoryService historyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("alert:history:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("userId", ShiroUtils.getUserId());
        PageUtils page = historyService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("alert:history:info")
    public R info(@PathVariable("id") Long id){
		HistoryEntity history = historyService.getById(id);

        return R.ok().put("history", history);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("alert:history:save")
    public R save(@RequestBody HistoryEntity history){
		historyService.save(history);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("alert:history:update")
    public R update(@RequestBody HistoryEntity history){
		historyService.updateById(history);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("alert:history:delete")
    public R delete(@RequestBody Long[] ids){
		historyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据实例id获得告警历史
     * @param instanceId
     * @return
     */
    @RequestMapping("/instance/{instanceId}")
    public R instance(@PathVariable("instanceId")String instanceId){
        List<HistoryEntity> historyEntities=historyService.getHistoryByInstanceId(instanceId);
        return R.ok().put("histories",historyEntities);
    }

    /**
     * 根据告警规则id获得告警历史
     * @return
     */
    @RequestMapping("/rule/{ruleId}")
    public R rule(@PathVariable("ruleId")Long ruleId){
        List<HistoryEntity> historyEntities=historyService.getHistoryByRuleId(ruleId);
        return R.ok().put("histories",historyEntities);
    }

}
